package com.dl.base.databind

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.dl.base.ui.image.imageLoader

//图片绑定适配器

//缩略图
@BindingAdapter("thumbnail", "thumbnailWidth", "thumbnailHeight", requireAll = true)
fun setImageThumbnail(imageView: ImageView, any: Any?, thumbnailWidth: Int, thumbnailHeight: Int) {
    any?.let {
        when (it) {
            is String -> imageLoader()
                .loadThumbnail(it, imageView, thumbnailWidth, thumbnailHeight)
        }
    }
}

//设置图片 + 默认图片 + 错误视图
@BindingAdapter(
    "src",
    "placeholder",
    "error",
    "radius",
    requireAll = false
)
fun ImageSrc(
    view: ImageView,
    src: Any?,
    placeholder: Drawable? = null,
    error: Drawable? = null,
    radius: Int = 0

) {
    src?.let {
        when (it) {
            is String -> imageLoader()
                .load(it, placeholder, error, view, radius)
            is Int -> imageLoader().load(it, view)
        }
    }
}